package model.auxillary;

import java.util.ArrayList;
import java.util.List;

//#Neu
/**
 * A {@code ParameterCollectionScaling} holds parameters of type FACILITY 
 * and FACTORY.
 * @author Sebastian Perlwitz
 *
 */
public class ParameterCollectionScaling {
	
	/**
	 * The name of the scaling that belongs to this 
	 * {@code ParameterCollectionScaling}.
	 */
	private String scalingName;
	
	/**
	 * Sub string of every parameter's id. 
	 */
	private String idString;
	
	/**
	 * Parameters of this collection.
	 */
	protected List<Parameter> parameters;	
	
	/**
	 * Identifying ID for a parameter 
	 */
	private long paramID;
	
	/**
	 * Number of parameters in this collection
	 */
	private int parametersCount;
	
	/**
	 * The ID of the scaling instance that holds this 
	 * {@code ParameterCollectionScaling}.
	 */
	private String scalingID;
	
	/**
	 * To create a new {@code ParameterCollectionScaling}.
	 * @param scalingName the name of the connected scaling
	 * @param idString an identifying ID.
	 * @param scalingID 
	 */
	public ParameterCollectionScaling(
			String scalingName,
			String idString,
			String scalingID) {
		this.scalingName = scalingName;
		this.idString = idString;
		this.scalingID = scalingID;
		parameters = new ArrayList<Parameter>();
		paramID = 0;
		parametersCount = 0;
	}
	
	/**
	 * To get the name of this {@code ParameterCollectionScaling}.
	 * @return the name.
	 */
	public String getName() {
		return scalingName;
	}
	
	/**
	 * To change the name of this collection's scaling name.
	 * @param facilityName the new name of the part process.
	 */
	public void setScalingName(String scalingName) {		
		this.scalingName = scalingName;
	}
	
	/**
	 * To get all parameters of this {@code ParameterCollectionScaling}.
	 * @return the parameters
	 */
	public List<Parameter> getParameters() {
		return parameters;
	}
	
	/**
	 * To add several parameters to this {@code ParameterCollectionScaling}.
	 * @param parameterList the parameters to be added
	 */
	public void addParameters(List<Parameter> parameterList) {
		for (Parameter param : parameterList) {
			param.setOrder(getParamCount());
			param.setKey(getNewID());
		}
		parameters.addAll(parameterList);
	}

	/**
	 * To get a certain parameter by it's identifying key.
	 * @param parameterKey the parameter's key.
	 * @return the parameter. Returns {@code null} when parameter does not
	 * exist.
	 */
	public Parameter getParameter(String parameterKey) {
		for (Parameter param : parameters) {
			if (param.getKey().equals(parameterKey)) {
				return param;
			}
		}
		return null;
	}
	
	/**
	 * To get a parameter by it's abbreviation.
	 * @param abbreviation the parameter's abbreviation	 
	 * @return the parameter. Returns {@code null} when parameter does not
	 * exist.
	 */
	public Parameter getParameterByAbbrev(String abbreviation) {
		for (Parameter param : parameters) {
			if (param.getAbbreviation().equals(abbreviation)) {
				return param;
			}
		}		
		return null;
	}
	
	/**
	 * To get a unique ID when creating a new parameter.
	 * @return
	 */
	public String getNewID() {
		return idString + "_" + paramID++;
	}

	/**
	 * To get the number of parameters inside this {@code ParameterCollection}.
	 * @return the number of parameters
	 */
	public int getParamCount() {
		return parametersCount;
	}	
	
	/**
	 * To get the number of parameters that must be set by the user.
	 * @return the number of user dependent parameters
	 */
	public int getUserSetParametersCount() {
		int count = 0;
		for (Parameter param : parameters) {
			if (param.isSetByUser()) {
				count++;
			}
		}
		return count;
	}
	
	/**
	 * Whether all parameters that have to be set by the user are set or not.
	 * @return <b>true</b> when all parameters are set
	 */
	public boolean allSet() {
		for (Parameter param : parameters) {
			if (param.isSetByUser() && !param.isSet()) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * To get the ID of the scaling that holds this
	 * {@code ParameterCollectionScaling}.
	 * @return scalingID
	 */
	public String getScalingID() {
		return scalingID;
	}
	
	//#USEFUL?
//	/**
//	 * To add a new parameter to this {@code ParameterCollectionScaling}.
//	 * @param param the parameter to be added
//	 */
//	public void addParameter(Parameter param) {
//		parameters.add(param);
//		parametersCount++;
//	}
	
//	/**
//	 * To get the ID of this {@code ParameterCollectionScaling}.
//	 * @return the ID
//	 */
//	public String getID() {
//		return idString;
//	}
	
//	@Override
//	public String toString() {
//		//return desc + scalingName;
//		return scalingName;
//	}
	
//	/**
//	 * To remove a certain parameter from this 
//	 * {@code ParameterCollectionScaling}.
//	 * @param param the parameter to be removed.
//	 */
//	public void removeParameter(Parameter param) {
//		if (parameters.contains(param)) {
//			parameters.remove(param);
//			parametersCount--;
//		} else {
//			throw new NoSuchElementException(
//					this + " does not hold " + param);
//		}		
//	}
	
}


